package com.example.leetcode.solution;

import java.util.*;

public class Solution17 {
    public static List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if (digits != null && !Objects.equals("", digits)) {
            Map<Object, String> charMap = new HashMap();
            charMap.put('0', "");
            charMap.put('1', "");
            charMap.put('2', "abc");
            charMap.put('3', "def");
            charMap.put('4', "ghi");
            charMap.put('5', "jkl");
            charMap.put('6', "mno");
            charMap.put('7', "pqrs");
            charMap.put('8', "tuv");
            charMap.put('9', "wxyz");
            conbinsString(charMap, digits, result, 0, new StringBuffer());
        }
        return result;
    }

    public static void conbinsString(Map<Object, String> charMap, String digits, List<String> result, int index, StringBuffer combination) {
        if (index == digits.length()) {
            result.add(combination.toString());
        } else {
            String letters = charMap.get(digits.charAt(index));
            for (int i = 0; i < letters.length(); i++) {
                combination.append(letters.charAt(i));
                conbinsString(charMap, digits, result, index + 1, combination);
                combination.deleteCharAt(index);
            }
        }
    }
}
